package com.shop.loan.proxy;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.shop.loan.mapper.LoanOrderBillByMybatisMapper;
import com.shop.loan.model.entity.LoanOrderBillByMybatis;
import com.shop.loan.model.entity.LoanOrderByMybatis;
import com.shop.loan.model.view.StatisticsByPeriodDay;
import com.shop.loan.util.Util;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.query.Condition;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsByPeriodDayProxy implements DataProxy<StatisticsByPeriodDay> {

    @Resource
    Util util;

    @Resource
    LoanOrderBillByMybatisMapper loanOrderBillByMybatisMapper;

    @Override
    public String beforeFetch(List<Condition> conditions) {
        conditions.removeIf(condition -> "channel".trim().equals(condition.getKey()));
        return null;
    }

    @Override
    public void afterFetch(Collection<Map<String, Object>> list) {
        String channelIdStr = util.getSearchParam("channel", 1);
        Long channelId = StringUtils.hasLength(channelIdStr) ? Long.valueOf(channelIdStr) : null;
        for (Map<String, Object> stringObjectMap : list) {
            String dayStr = stringObjectMap.get("day").toString();
            DateTime dayTime = DateUtil.parse(dayStr);
            String timeStartStr = DateUtil.beginOfDay(dayTime).toString();
            String timeEndStr = DateUtil.endOfDay(dayTime).toString();

            //一期订单数、一期订单总额、一期已还订单数，一期已还金额，一期还款率
            //二期订单数、二期订单总额、二期已还订单数，二期已还金额，二期还款率，一期还了二期未还数量，一期还了二期未还比例
            LoanOrderByMybatis loanOrderByMybatis = new LoanOrderByMybatis();
            loanOrderByMybatis.setStartTime(timeStartStr);
            loanOrderByMybatis.setEndTime(timeEndStr);
            loanOrderByMybatis.setChannelId(channelId);
            loanOrderByMybatis.setUserType(Integer.parseInt(stringObjectMap.get("type").toString()));
            loanOrderByMybatis.setPeriod(1);
            Integer totalOrderNum_1 = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);
            loanOrderByMybatis.setPayStatus(1);
            Integer totalOrderNumPay_1 = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);
            loanOrderByMybatis.setPayStatus(null);
            LoanOrderBillByMybatis billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
            if (billSum == null || billSum.getMoney() == null) {
                billSum = new LoanOrderBillByMybatis();
                billSum.setMoney(BigDecimal.ZERO);
                billSum.setPayMoney(BigDecimal.ZERO);
                billSum.setDiscountsMoney(BigDecimal.ZERO);
                billSum.setRateMoney(BigDecimal.ZERO);
                billSum.setOverdueFine(BigDecimal.ZERO);
            }

            BigDecimal totalMoney_1 = billSum.getMoney().add(billSum.getRateMoney());
            BigDecimal payMoney_1 = billSum.getPayMoney();
            BigDecimal payRate_1 = payMoney_1.divide(totalMoney_1.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ONE : totalMoney_1, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));

            loanOrderByMybatis.setPeriod(2);
            Integer totalOrderNum_2 = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);
            totalOrderNum_2 = totalOrderNum_2 == null ? 0 : totalOrderNum_2;
            loanOrderByMybatis.setPayStatus(1);
            Integer totalOrderNumPay_2 = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);

            //一期结束且二期未还
            loanOrderByMybatis.setPeriod_end_1(1);
            loanOrderByMybatis.setPayStatus(0);
            Integer period_end_num_2 = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);
            period_end_num_2 = period_end_num_2 == null ? 0 : period_end_num_2;
            BigDecimal period_end_rate_2 = BigDecimal.valueOf(period_end_num_2).divide(BigDecimal.valueOf(totalOrderNum_2 == 0 ? 1 : totalOrderNum_2),2, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));

            loanOrderByMybatis.setPayStatus(null);
            billSum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
            if (billSum == null || billSum.getMoney() == null) {
                billSum = new LoanOrderBillByMybatis();
                billSum.setMoney(BigDecimal.ZERO);
                billSum.setPayMoney(BigDecimal.ZERO);
                billSum.setDiscountsMoney(BigDecimal.ZERO);
                billSum.setRateMoney(BigDecimal.ZERO);
                billSum.setOverdueFine(BigDecimal.ZERO);
            }
            BigDecimal totalMoney_2 = billSum.getMoney().add(billSum.getRateMoney());
            BigDecimal payMoney_2 = billSum.getPayMoney();
            BigDecimal payRate_2 = payMoney_2.divide(totalMoney_2.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ONE : totalMoney_2, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));


            stringObjectMap.put("totalOrderNum_1", totalOrderNum_1 == null ? 0 : totalOrderNum_1);
            stringObjectMap.put("totalOrderNumPay_1", totalOrderNumPay_1 == null ? 0 : totalOrderNumPay_1);
            stringObjectMap.put("totalMoney_1", totalMoney_1);
            stringObjectMap.put("payMoney_1", payMoney_1);
            stringObjectMap.put("payRate_1", payRate_1);


            stringObjectMap.put("totalOrderNum_2", totalOrderNum_2);
            stringObjectMap.put("totalOrderNumPay_2", totalOrderNumPay_2 == null ? 0 : totalOrderNumPay_2);
            stringObjectMap.put("totalMoney_2", totalMoney_2);
            stringObjectMap.put("payMoney_2", payMoney_2);
            stringObjectMap.put("payRate_2", payRate_2);
            stringObjectMap.put("period_end_num_2", period_end_num_2);
            stringObjectMap.put("period_end_rate_2", period_end_rate_2);


        }
    }

}
